<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\OrderRepository;
use App\Repositories\UsersRepository;
use App\Repositories\UserslogRepository;
use App\Repositories\PayRepository;
use App\Traits\BaseFun;
use Zhifu\Juhe;

class OrderController extends Controller
{
	public function __construct(OrderRepository $OrderRepository,UsersRepository $UsersRepository,UserslogRepository $UserslogRepository,PayRepository $PayRepository)
    {
        $this->OrderRepository=$OrderRepository;
        $this->UsersRepository=$UsersRepository;
        $this->UserslogRepository=$UserslogRepository;
        $this->PayRepository=$PayRepository;
    }
    public function Order(Request $request)
    {   
        $where=[];
        if ($request->search) {
            $where['search']=$request->search;
        }
        if ($request->start_time && $request->end_time) {
            $where['start_time']=$request->start_time.' 00:00:01';
            $where['end_time']=$request->end_time.' 00:00:01';
        }
        $res = $this->OrderRepository->pageList($where);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    
    public function OrderEdit(Request $request,$id)
    {
        $res=$this->OrderRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function OrderUpdate(Request $request)
    {
        $res=$this->OrderRepository->update($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '修改成功!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }

    public function OrderBack(Request $request,$id)
    {   
        $data=[];
        $orderInfo=$this->OrderRepository->info(['id'=>$id]);
        $userInfo=$this->UsersRepository->info(['id'=>$orderInfo['userid']]);
        if ($orderInfo['type']==1) {
            $data['number']=$orderInfo['order_sn'];
            $data['refundNumber']=$orderInfo['order_sn'];
            $data['totalFee']=$orderInfo['price'];
            $data['refundFee']=$orderInfo['price'];
            // $backPay=$this->PayRepository->backPay($data);
            $zhifu=new Juhe();
            $backPay=$zhifu->cancel($data);
            // var_dump($backPay);die;
            if ($backPay['retcode']==='SUCCESS') {
                $back=$this->OrderRepository->update(['id'=>$id,'state'=>4]);
                $this->UserslogRepository->create(['order_sn'=>$orderInfo['order_sn'],'price'=>$orderInfo['price'],'userid'=>$orderInfo['userid'],'type'=>3,'des'=>'平台微信退款订单']);
            }else{
                $back='';
            }
        }else{
            $back=$this->OrderRepository->update(['id'=>$id,'state'=>4]);
            if ($back) {
                $this->UsersRepository->update(['id'=>$orderInfo['userid'],'balance'=>$userInfo['balance']+$orderInfo['price']]);
                $this->UserslogRepository->create(['order_sn'=>$orderInfo['order_sn'],'price'=>$orderInfo['price'],'userid'=>$orderInfo['userid'],'type'=>3,'des'=>'平台余额退款订单']);
            }
        }
        if ($back) {
           $ArrDate = ['state' => 0, 'data' => $back, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $back, 'msg' => '退款失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }

}
